import PolylineImageTrailMaterialProperty from "./PolylineImageTrailMaterialProperty";
import CircleDiffuseMaterialProperty from "./CircleDiffuseMaterialProperty";
import arrow from './arrow.png'
export default class Rader{
    _viewer:any
    constructor(viewer:any){
        this._viewer=viewer
    }
    create(){
        let viewer=this._viewer
        const position = Cesium.Cartesian3.fromDegrees(110, 40, 100);
        let num = 0;
        const _heading = Cesium.Math.toRadians(num); //水平旋转
        const _pitch = Cesium.Math.toRadians(0.0);
        const _roll = Cesium.Math.toRadians(0.0);
        let _headingPitchRoll = new Cesium.HeadingPitchRoll(
          _heading,
          _pitch,
          _roll
        );
        let _orientation = Cesium.Transforms.headingPitchRollQuaternion(
          position,
          _headingPitchRoll
        );
   
        let model = viewer.entities.add({
          position: position,
          orientation: _orientation,
          model: {
            uri: `${window.location.origin}/data/model/zhui.glb`,
            scale: 300,
          },
        });
        setInterval(() => {
          num += 4;
          if (num >= 360 || num <= -360) {
            num = 0;
          }
          model.orientation = Cesium.Transforms.headingPitchRollQuaternion(
            Cesium.Cartesian3.fromDegrees(110, 40, 100),
            new Cesium.HeadingPitchRoll(
              Cesium.Math.toRadians(num),
              Cesium.Math.toRadians(0),
              Cesium.Math.toRadians(0)
            )
          );
        }, 50);
        const options = [
          {
            raius: 500,
            clockwise: false,
            color: "#ff000090",
          },
          { raius: 1000, clockwise: true, color: "#ffa50090" },
          { raius: 1500, clockwise: false, color: "#9acd3290" },
        ];
   
        for (let index = 0; index < options.length; index++) {
          const option = options[index];
          this.createCircle([110, 40, 100], option, options[index + 1]);
        }
        viewer.camera.flyTo({
          destination: new Cesium.Cartesian3(
            -1675652.7617794448,
            4603525.7275218675,
            4083007.3599704667
          ),
          orientation: {
            heading: 0.09085393067733083,
            pitch: -1.5329743385768886,
            roll: 0.0,
          },
          duration: 3,
        });
    }
    createCircle(pos:any, option:any, bool:any) {
        let viewer=this._viewer

        const radius = option.raius;
        const num = Math.ceil(radius / 10);
        let positions:any = [];
        let pos_0:any = [];
        for (let i = 0; i < 360; i++) {
          const _pos:any = this.get_another_point(pos[0], pos[1], i, radius);
          i == 0 && (pos_0 as any).push(_pos.lng, _pos.lat);
          option.clockwise
            ? (positions as any).push(_pos.lng, _pos.lat)
            :(positions as any).unshift(_pos.lng, _pos.lat);
        }
        positions.push(pos_0[0], pos_0[1]);
        viewer.entities.add({
          polyline: {
            clampToGround: true,
            positions: Cesium.Cartesian3.fromDegreesArray(positions),
            material: new PolylineImageTrailMaterialProperty({
              color: Cesium.Color.fromCssColorString(option.color),
              speed: 30,
              image: arrow,
              repeat: { x: num, y: 1 },
            }),
            width: 5,
          },
        });
        !bool &&
          viewer.entities.add({
            polygon: {
              hierarchy: new Cesium.PolygonHierarchy(
                Cesium.Cartesian3.fromDegreesArray(positions)
              ),
              material: new CircleDiffuseMaterialProperty({
                color: Cesium.Color.RED,
                speed: 10,
              }),
            },
          });
      }
      get_another_point(lng:any, lat:any, angle:any, distance:any) {
        let viewer=this._viewer

        // WGS84坐标系
        var a = 6378137; // 赤道半径
        var b = 6356752.3142; // 短半径
        var f = 1 / 298.257223563; // 扁率
   
        var alpha1 = angle * (Math.PI / 180);
        var sinAlpha1 = Math.sin(alpha1);
        var cosAlpha1 = Math.cos(alpha1);
        var tanU1 = (1 - f) * Math.tan(lat * (Math.PI / 180));
        var cosU1 = 1 / Math.sqrt(1 + tanU1 * tanU1),
          sinU1 = tanU1 * cosU1;
        var sigma1 = Math.atan2(tanU1, cosAlpha1);
        var sinAlpha = cosU1 * sinAlpha1;
        var cosSqAlpha = 1 - sinAlpha * sinAlpha;
        var uSq = (cosSqAlpha * (a * a - b * b)) / (b * b);
        var A =
          1 + (uSq / 16384) * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
        var B = (uSq / 1024) * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
        var sigma = distance / (b * A),
          sigmaP = 2 * Math.PI;
          var sinSigma :any
          var cosSigma:any
           var cos2SigmaM:any
        while (Math.abs(sigma - sigmaP) > 1e-12) {
           cos2SigmaM = Math.cos(2 * sigma1 + sigma);
           sinSigma = Math.sin(sigma);
           cosSigma = Math.cos(sigma);
          var deltaSigma =
            B *
            sinSigma *
            (cos2SigmaM +
              (B / 4) *
                (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) -
                  (B / 6) *
                    cos2SigmaM *
                    (-3 + 4 * sinSigma * sinSigma) *
                    (-3 + 4 * cos2SigmaM * cos2SigmaM)));
          sigmaP = sigma;
          sigma = distance / (b * A) + deltaSigma;
        }
   
        var tmp = sinU1 * sinSigma - cosU1 * cosSigma * cosAlpha1;
        var lat2 = Math.atan2(
          sinU1 * cosSigma + cosU1 * sinSigma * cosAlpha1,
          (1 - f) * Math.sqrt(sinAlpha * sinAlpha + tmp * tmp)
        );
        var lambda = Math.atan2(
          sinSigma * sinAlpha1,
          cosU1 * cosSigma - sinU1 * sinSigma * cosAlpha1
        );
        var C = (f / 16) * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
        var L =
          lambda -
          (1 - C) *
            f *
            sinAlpha *
            (sigma +
              C *
                sinSigma *
                (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
        return {
          lng: Number(lng) + L * (180 / Math.PI),
          lat: lat2 * (180 / Math.PI),
        };
      }
      getLocation() {
        let viewer=this._viewer

        let handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
        handler.setInputAction(function (event:any) {
          let earthPosition = viewer.scene.pickPosition(event.position);
          if (Cesium.defined(earthPosition)) {
            let cartographic = Cesium.Cartographic.fromCartesian(earthPosition);
            let lng = Cesium.Math.toDegrees(cartographic.longitude).toFixed(5);
            let lat = Cesium.Math.toDegrees(cartographic.latitude).toFixed(5);
            let height = cartographic.height.toFixed(2);
            console.log(earthPosition, {
              lng: lng,
              lat: lat,
              height: height,
            });
          }
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
      }
}